﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ufida.u9.ait.openapi.utils;
using UFIDA.U9.Base;
using UFIDA.U9.Base.Organization;
using UFIDA.U9.CBO.FI.BankAccount;
using UFIDA.U9.CBO.FI.CashAccount;
using UFIDA.U9.CBO.FI.IncExpItem;
using UFIDA.U9.CBO.FI.SettlementMethod;
using UFIDA.U9.CBO.HR.Department;
using UFIDA.U9.CBO.HR.Operator;
using UFIDA.U9.CBO.SCM.Customer;
using UFIDA.U9.CBO.SCM.ProjectTask;
using UFIDA.U9.CBO.SCM.Supplier;
using UFIDA.U9.CM.CMBill;
using UFIDA.U9.CM.CMBill.PayBill;
using UFIDA.U9.CM.CMBillBP;
using UFSoft.UBF.Business;

namespace ufida.u9.ait.openapi
{
    class CMPayBillHelper
    {
		public static CMPayBill CreateDoc(CMPayBillAITDTOData dto)
		{
			CMPayBill doc = null;
			//校验
			if (dto.PayObjType < 0)
				throw new Exception("PayObjType必填");
			if (dto.PayObjType == 0 && dto.CustomerSite == null)
				throw new Exception("付款对象类型=客户,CustomerSite必填");
			else if (dto.PayObjType == 1 && dto.SupplierSite == null)
				throw new Exception("付款对象类型=客户,SupplierSite必填");
			else if (dto.PayObjType == 2)
				throw new Exception("付款对象类型=部门,暂不支持");
			else if (dto.PayObjType == 3)
				throw new Exception("付款对象类型=员工,暂不支持");

			if (dto.SettleMethod == null)
				throw new Exception("SettleMethod必填");
			if (dto.DocumentType == null)
				throw new Exception("DocumentType必填");

			//创建实体
			using (ISession session = Session.Open())
			{
				doc = CMPayBill.Create();

				doc.SrcSys = UFIDA.U9.CM.Enums.SrcSysEnum.GetFromValue(4);
				if (dto.Org == null)
				{
					doc.Org = Context.LoginOrg;
				}
				else
				{
					doc.Org = EntityUtils.GetEntityByCode<Organization>(dto.Org.Code);
				}
				doc.SourceOrg = doc.Org;
				doc.SettleOrg = doc.Org;
				doc.BizOrg = doc.Org;
				doc.DocumentType = EntityUtils.GetEntityByCode<CMPayDocType>(dto.DocumentType);
				//供应商
				if (dto.SupplierSite != null)
				{
					SupplierSite site = EntityUtils.GetEntityByCode<SupplierSite>(dto.SupplierSite.Code);
					doc.SupplierSite = new SupplierSiteMISCInfo();
					doc.SupplierSite.SupplierSite = site;
					doc.SupplierSite.Code = site.Code;
					doc.SupplierSite.Name = site.Name;
					doc.Supplier = new SupplierMISCInfo();
					doc.Supplier.Supplier = site.Supplier;
					doc.Supplier.Code = site.Supplier.Code;
					doc.Supplier.Name = site.Supplier.Name;
				}
				//客户
				if (dto.CustomerSite != null)
				{
					CustomerSite site = EntityUtils.GetEntityByCode<CustomerSite>(dto.CustomerSite.Code);
					doc.CustomerSite = new CustomerSiteMISCInfo();
					doc.CustomerSite.CustomerSite = site;
					doc.CustomerSite.Code = site.Code;
					doc.CustomerSite.Name = site.Name;
					doc.Customer = new CustomerMISCInfo();
					doc.Customer.Customer = site.Customer;
					doc.Customer.Code = site.Customer.Code;
					doc.Customer.Name = site.Customer.Name;
					doc.Customer.ShortName = site.Customer.ShortName;
				}
				//收款银行
				if (dto.RcvBankID != null)
				{
					doc.RcvBankID = EntityUtils.GetEntityByCode<Bank>(dto.RcvBankID.Code);
					doc.RcvBank = doc.RcvBankID.Name;
					doc.RcvBankAcc = dto.RcvBankAcc;
					doc.RcvAccName = dto.RcvAccName;
				}
				//付款银行
				if (dto.PayBank != null)
				{
					doc.PayBank = EntityUtils.GetEntityByCode<Bank>(dto.PayBank.Code);
					doc.PayBankAcc = EntityUtils.GetEntityByCode<BankAccount>(dto.PayBankAcc);
					doc.PayAccName = doc.PayBankAcc.Name;
				}

				//日期处理
				if (String.IsNullOrEmpty(dto.BusinessDate))
				{
					doc.BusinessDate = DateTime.Now;
				}
				else
				{
					doc.BusinessDate = DateTime.Parse(dto.BusinessDate);
				}
				doc.PostPeriod = EntityUtils.getPostPeriod(doc.BusinessDate);

				//金额/币种处理
				if (dto.PayCurrency == null)
				{
					dto.PayCurrency = new CopyOfCommonArchiveDataDTOData
					{
						Code = "C001",
					};
				}
				doc.PayCurrency = EntityUtils.GetEntityByCode<UFIDA.U9.Base.Currency.Currency>(dto.PayCurrency);
				doc.FC = doc.PayCurrency;
				doc.AccCurrency = doc.PayCurrency;
				doc.PayMoney = dto.PayMoney;
				doc.PayFCMoney = dto.PayMoney;
				doc.AccMoney = dto.PayMoney;
				doc.AccFCMoney = dto.PayMoney;
				//扩展字段处理
				if (dto.DescFlexField != null)
				{
					doc.DescFlexField = new UFIDA.U9.Base.FlexField.DescFlexField.DescFlexSegments();
					DTOUtils.DTOPropCopy(dto.DescFlexField, doc.DescFlexField);
				}
				//其他处理
				doc.DocNo = dto.DocNo;
				doc.WorkFlowID = Guid.Empty;
				doc.Project = EntityUtils.GetEntityByCode<Project>(dto.Project);
				doc.Dept = EntityUtils.GetEntityByCode<Department>(dto.Dept);
				doc.BusinessMan = EntityUtils.GetEntityByCode<Operators>(dto.BusinessMan);
				doc.SettleMethod = EntityUtils.GetEntityByCode<SettlementMethod>(dto.SettleMethod.Code);
				doc.PayObjType = UFIDA.U9.CBO.FI.Enums.RecPayObjectTypeEnum.GetFromValue(dto.PayObjType);
				doc.CashAccount = EntityUtils.GetEntityByCode<CashAccount>(dto.CashAccount);
				doc.IncExpItem=EntityUtils.GetEntityByCode<IncExpItem>(dto.IncExpItem);

				doc.PayFCRate = 1;
				doc.PayAccRate = 1;
				doc.AccFCRate = 1;
				doc.Memo = dto.Memo;

				session.Commit();
			}

			return doc;
		}

		public static CMPayBill ApproveDoc(CMPayBill doc)
		{
			SubmitBillBP submit = new SubmitBillBP();
			submit.BillID = doc.ID;
			submit.BusinessType = UFIDA.U9.Base.Doc.BusinessTypeEnum.CM_Pay;
			submit.CurrentSysVersion = doc.SysVersion;
			submit.Do();

			doc = CMPayBill.Finder.FindByID(doc.ID);

			ApproveBillBP approve = new ApproveBillBP();
			approve.BillID = doc.ID;
			approve.BusinessType = UFIDA.U9.Base.Doc.BusinessTypeEnum.CM_Pay;
			approve.CurrentSysVersion = doc.SysVersion;
			string ret = approve.Do();

			return doc;
		}
	}
}
